<?php
// +----------------------------
// | Created by PhpStorm
// +----------------------------
// | Author: YnnSon
// +----------------------------
namespace app\api\model;

use app\lib\exception\MissException;
use think\Db;

/**
 * 检车站
 * Class Company
 * @package app\api\model
 */
class Company extends BaseModel
{
    /**
     * @Desc 定义全局的前置查询范围
     *
     * @param $query
     */
    protected function base($query)
    {
        $query->where('is_use', '=', 1)->where('is_delete', '=', 0)->whereNotNull('company_number')->order('sort, create_time');
    }

    /**
     * @Desc 获取检车项目
     *
     * @param $value
     * @param $data
     *
     * @return mixed
     */
    protected function getInspectionAttr($value, $data)
    {
        return VehicleInspectionCompany::getInspectionList($data['id'], $data['vehicle_ids']);
    }

    /**
     * @Desc 获取检车项目
     * @return mixed
     */
    protected function getSubscribeIsOpenAttr()
    {
        return Db::name('base_subscribe_num')->where('company_id', '=', 0)->value('is_use');
    }

    /**
     * @Desc 为图片添加URL前缀
     *
     * @param $value
     *
     * @return string
     */
    protected function getLogoUrlAttr($value)
    {
        return $this->addWebUrlPrefixToImage($value);
    }

    /**
     * @Desc 根据companyname获取公司列表
     *
     * @param array $param 前端参数
     *
     * @return array
     */
    public static function getList(array $param)
    {
        $field = 'id, company_name, company_number, longitude, latitude, start_time, end_time';
        $where = [];
        if (!empty($param['company_name'])) {
            $where['company_name|company_addr'] = ['like', "%{$param['company_name']}%"];
        }
        if (!empty($param['district_id'])) {
            $where['city_id'] = Db::name('base_citys')->where('id', '=', $param['district_id'])->value('parentid');
        }
        return (new self)->field($field)->where($where)->select()->toArray();
    }

    /**
     * @Desc 获取检车站详情
     *
     * @param int $id
     *
     * @return array
     */
    public static function getDetail($id)
    {
        $field = 'id, company_name, company_number, logo_url, company_addr, longitude, latitude, telephone, vehicle_ids, start_time, end_time';
        $company = (new self)->field($field)->where('id', '=', $id)->find();
        if (!$company) {
            throw new MissException([
                'message'   => '该检车站不存在',
                'errorCode' => 20000,
            ]);
        }
        return $company->append(['inspection', 'subscribe_is_open'])->hidden(['vehicle_ids'])->toArray();
    }

    /**
     * @Desc 收银系统 - 获取检车站列表
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public static function getListToPayment()
    {
        return (new self)->field('id, company_name')->select();
    }

    /**
     * @Desc 根据companyname获取公司列表
     *
     * @param array $param 前端参数
     *
     * @return Company[]|false
     */
    public static function getList2(array $param)
    {
        $field = 'id, company_name, logo_url, company_addr, longitude, latitude, vehicle_ids, start_time, end_time';
        $where = [];
        if (isset($param['districtId']) && 0 != $param['districtId']) {
            $where['district'] = $param['districtId'];
        }
        if (isset($param['vehicleId']) && 0 != $param['vehicleId']) {
            $where['vehicle_ids'] = ['like', "%{$param['vehicleId']}%"];
        }
        if (!empty($param['company_name'])) {
            $where['company_name|company_addr'] = ['like', "%{$param['company_name']}%"];
        }
        $list = (new self)->field($field)->where($where)->select();
        return $list->append(['vehicle_type'])->toArray();
    }

    /**
     * @Desc vehicle_ids数据处理
     *
     * @param $value
     * @param $data
     *
     * @return string
     */
    protected function getVehicleTypeAttr($value, $data)
    {
        $vehicle = Db::name('vehicle')->whereIn('id', $data['vehicle_ids'])->column('sm_title');
        return implode(',', $vehicle);
    }
}